<template>
  <div>
    <!-- 分享弹窗 -->
    <t-modal
      title="分享场景"
      width="500px"
      :visible="visible"
      @cancel="closeModal()"
      wrapClassName="sharing-scene-container"
      forceRender
    >
      <div class="sharing-modal-body">
        <a-form-model
          ref="ruleForm"
          :model="form"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >
          <a-form-model-item ref="name" label="场景名称" prop="name">
            <div class="name-warp">
              {{ form.name }}
            </div>
          </a-form-model-item>
          <a-form-model-item ref="validity" label="有效期" prop="validity">
            <a-radio-group v-model="form.validity">
              <a-radio :value="7">
                7天
              </a-radio>
              <a-radio :value="30">
                30天
              </a-radio>
              <a-radio :value="0">
                永久
              </a-radio>
            </a-radio-group>
          </a-form-model-item>
          <a-form-model-item ref="desc" label="备注" prop="desc">
            <a-input v-model="form.desc" type="textarea" />
          </a-form-model-item>
        </a-form-model>
      </div>
      <template slot="footer">
        <a-button @click="closeModal()">取消</a-button>
        <a-button type="primary" :loading="loading" @click="createLink()">
          创建链接
        </a-button>
      </template>
    </t-modal>
    <!-- 链接弹窗 -->
    <t-modal
      title="场景链接"
      width="500px"
      :visible="linkVisible"
      @cancel="closeLinkModal()"
      wrapClassName="sharing-scene-link-container"
      forceRender
    >
      <div class="sharing-modal-body">
        <a-form-model
          ref="ruleForm"
          :model="form"
          :label-col="{ span: 3 }"
          :wrapper-col="{ span: 20 }"
        >
          <a-form-model-item ref="url" label="链接" prop="url">
            <div class="form-item-warp">
              <a-tooltip>
                <template slot="title">
                  {{ form.url }}
                </template>
                <div class="link-warp">
                  {{ form.url }}
                </div>
              </a-tooltip>
              <a-button
                type="primary"
                class="copy-btn"
                data-clipboard-action="copy"
                :data-clipboard-text="form.url"
                @click="copyLinkUrl()"
                >复制链接</a-button
              >
            </div>
            <div class="link-tip">
              {{
                form.validity
                  ? `链接将于${form.validity}天后失效`
                  : "链接永久有效"
              }}
            </div>
          </a-form-model-item>
        </a-form-model>
      </div>
      <template slot="footer">
        <a-button @click="closeLinkModal()">取消</a-button>
      </template>
    </t-modal>
  </div>
</template>

<script>
import moment from "moment";
import Clipboard from "clipboard";
import { randomUUID } from "@/utils/util.js";
import { axios } from "@/utils/request";
export default {
  name: "shareModal",
  inject: ["currentProject", "sceneSource"],
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      linkVisible: false,
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      other: "",
      form: {
        //场景名称
        name: "",
        //有效期
        validity: 7,
        //备注
        desc: "",
        //
        url: "",
      },
    };
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.form.name = this.sceneSource()?.sceneName ?? "";
        }
      },
      immediate: true,
    },
  },
  mounted() {},
  methods: {
    //创建链接
    createLink() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let uuid = randomUUID();
          let { foreignUuid, orgCode } = this.currentProject();
          let { sceneId } = this.sceneSource();
          this.loading = true;
          axios
            .post(
              "/probusiness/bimShare/save",
              {
                uuid,
                proCode: orgCode,
                sceneId,
                projid: foreignUuid,
                expireDate: this.calcValidity(),
                remark: this.form.desc,
              },
              {
                headers: {
                  "Content-Type": "application/json",
                },
              }
            )
            .then((res) => {
              let { statusCode } = res;
              if (statusCode == 200) {
                this.closeModal(1);
                let domain = location.origin;
                this.form.url = `${domain}/#/blackHole/shareSceneView?uuid=${uuid}`;
                this.linkVisible = true;
              } else {
                this.$message.error("链接生成失败，请稍后重试！");
              }
            })
            .finally((_) => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },

    //计算链接有效期
    calcValidity() {
      let { validity } = this.form;
      if (!validity) {
        return 0;
      } else {
        let today = moment();
        let futureDate = today.add(validity, "days");
        return futureDate.format("YYYY-MM-DD");
      }
    },

    //复制链接
    copyLinkUrl() {
      if (!this.form.url) {
        this.$message.warning("没有文本可复制");
        return;
      }
      let clipboard = new Clipboard(".copy-btn");
      clipboard.on("success", () => {
        this.$message.success("链接复制成功");
      });
      clipboard.on("error", () => {
        this.$message.error("链接复制失败，请手动复制！");
      });
      setTimeout(() => {
        // 销毁实例
        clipboard.destroy();
      }, 122);
    },

    //关闭
    closeModal(type = 0) {
      this.$emit("update:visible", false);
      if (!type) {
        this.$emit("close");
      }
    },

    closeLinkModal() {
      this.linkVisible = false;
      this.$emit("close");
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.sharing-scene-container {
  .sharing-modal-body {
    display: flex;
    flex-direction: column;
    .name-warp {
      width: 100%;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}

/deep/.sharing-scene-link-container {
  .sharing-modal-body {
    .form-item-warp {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      .link-warp {
        flex: 1;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        border: 1px solid #d9d9d9;
        border-radius: 4px;
        padding: 0 4px;
        background: rgb(245, 247, 250);
        height: 36px;
        line-height: 32px;
      }

      button {
        flex-shrink: 0;
        margin-left: 10px;
      }
    }
    .link-tip {
      color: #ff993f;
      margin-top: -4px;
    }
  }
}
</style>
